package com.nervenets.web.controller.handler.admin.movie;

import com.alibaba.fastjson.JSON;
import com.nervenets.utils.JodaUtils;
import com.nervenets.web.controller.handler.SessionHandler;
import com.nervenets.web.entity.MessageCode;
import com.nervenets.web.enumeration.MovieInfoStatus;
import com.nervenets.web.enumeration.Role;
import com.nervenets.web.hibernate.domain.film.Movie;
import com.nervenets.web.hibernate.domain.film.MovieCheckInfo;
import com.nervenets.web.hibernate.domain.user.User;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;

public class MovieEditHandler extends SessionHandler<MovieEditParams> {
    @Override
    public ArrayList<Role> allowRoles() {
        return Role.issues();
    }

    @Override
    public boolean needLog() {
        return false;
    }

    @Override
    public String handle(MovieEditParams params, HttpServletRequest request) throws Exception {
        final User user = userDao.findById(params.getTargetUserId(), User.class);
        if (null == user) return errorMessage(MessageCode.code_404, "发行商用户ID不存在");

        if (null == params.getStatus()) return errorMessage(MessageCode.code_444, "融资进度不能为空");
        if (null == params.getReleaseStatus()) return errorMessage(MessageCode.code_444, "上映状态不能为空");
        if (null == params.getType()) return errorMessage(MessageCode.code_444, "影视类型不能为空");

        Movie movie = new Movie();
        if (params.getMovieId() > 0) {
            movie = userDao.findById(params.getMovieId(), Movie.class);
            if (null == movie) return errorMessage(MessageCode.code_404, "您要编辑的影片不存在");
        }

        //final long beforePriceOfUnit = movie.getInfo().getPriceOfUnit();
        //final MovieStatus beforeStatus = movie.getStatus();

        if (movie.isNew()) {
            movie.setTotalOfMoney(params.getTotalOfMoney());
        }
        movie.setType(params.getType());
        movie.setUserId(params.getTargetUserId());
        //movie.setStatus(params.getStatus());
        //movie.setReleaseStatus(params.getReleaseStatus());
        movie.setQueue(params.getQueue());
        movie.setDisable(params.isDisable() ? JodaUtils.getTimestamp() : 0);

        MovieCheckInfo check = new MovieCheckInfo(MovieInfoStatus.checking);
        check.setTitle(params.getTitle());
        check.setSubtitle(params.getSubtitle());
        check.setPriceOfUnit(params.getPriceOfUnit());
        check.setUserLimit(params.getUserLimit());
        check.setDeposit(params.getDeposit());
        check.setCoverImage(params.getCoverImage());
        check.setCarousels(JSON.parseArray(params.getCarousels(), String.class));
        check.setContent(params.getContent());
        check.setIncomeDetail(params.getIncomeDetail());
        check.setContractDetail(params.getContractDetail());
        check.setRiskWarning(params.getRiskWarning());
        check.setUserId(params.getUserId());
        check.setMovieStatus(params.getStatus());
        check.setReleaseStatus(params.getReleaseStatus());
        //剩余金额与单份价格不能整除，这里需要读取已卖金额来判断
        long salesOfMoney = movie.isNew() ? 0 : globalService.getMovieBuyMoney(movie.getId());
        final long leftMoney = params.getTotalOfMoney() - salesOfMoney;
        if (leftMoney % params.getPriceOfUnit() != 0) {
            return errorMessage(MessageCode.code_444, String.format("剩余金额[%s]与单份价格[%s]不能整除", leftMoney, params.getPriceOfUnit()));
        }

        movie.setCheckInfo(check);

        userDao.saveOrUpdate(movie);

        globalService.clearRedisMovieStored(movie.getId());

        return successMessage();
    }

    @Override
    public String result() throws Exception {
        return successMessage();
    }
}
